package Server.Queue;

import Client.Client;
import Const.ServerConst;
import Server.ServerClientInstance;
import java.io.Serializable;
import java.util.LinkedList;

/**
 *
 * Class description goes here.
 *
 * @author Kamil Gajowy
 */
public class PersonQueue implements Serializable {

    protected LinkedList<ServerClientInstance> clientList;
    protected int myType;
    protected int capacity;
    
    /**
    * Default constructor.
     * @param queueType
     */
    public PersonQueue(int queueType){
        switch(queueType){
            case ServerConst.QueueLimited:
                myType = ServerConst.QueueLimited;
                capacity = ServerConst.LiftMaxPerson;
                break;
            default:
                myType = ServerConst.QueueInfinity;
                capacity = ServerConst.LiftInfinityPerson;
        }
        clientList = new LinkedList<ServerClientInstance>();
    }//end c-tor

    /**
     * Dodaje osobe do kolejki.
     * @param client
     * @return
     */
    public boolean addPerson(ServerClientInstance client){
        if ( myType == ServerConst.QueueLimited
                && getClientList().size() >= capacity ){
            return false;
        }
        else{
            int i;
            for ( i = 0 ; i < getClientList().size() ; ++i ){
                if (getClientList().get(i).getClient().getId() ==
                        client.getClient().getId()){
                    return false;
                }
            }
            getClientList().add(client);
            System.out.println("\tClient " + client.getSocket().getInetAddress().getHostAddress()
                    + " joined the crowd in the lift.");
            return true;
        }
    }//end addPerson

    public void removePerson(ServerClientInstance client){
        //TODO delete improve?
        int i;
        for ( i = 0 ; i < getClientList().size() ; ++i ){
            if ( getClientList().get(i).getClient().getId() ==
                    client.getClient().getId()){
                getClientList().remove(client);
                break;
            }
        }
        
    }

    /**
     * @return the clientList
     */
    public LinkedList<ServerClientInstance> getClientList() {
        return clientList;
    }

}//end class
